1   /*
2    * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   *
26   */
27  
28  package bench.serial;
29  
30  import bench.Benchmark;
31  import java.io.ObjectInputStream;
32  import java.io.ObjectOutputStream;
33  import java.io.Serializable;
34  import java.lang.reflect.Constructor;
35  import java.lang.reflect.InvocationHandler;
36  import java.lang.reflect.Method;
37  import java.lang.reflect.Proxy;
38  
39  /**
40   * Benchmark for testing speed of proxy array reads/writes.
41   */
42  public class ProxyArrays implements Benchmark {
43  
44      static class DummyHandler implements InvocationHandler, Serializable {
45          public Object invoke(Object proxy, Method method, Object[] args)
46              throws Throwable
47          {
48              return null;
49          }
50      }
51  
52      static interface DummyInterface {
53          public void foo();
54      }
55  
56      /**
57       * Write and read proxy arrays to/from a stream.  The benchmark is run in
58       * batches, with each batch consisting of a fixed number of read/write
59       * cycles.  The ObjectOutputStream is reset after each batch of cycles has
60       * completed.
61       * Arguments: <array size> <# batches> <# cycles per batch>
62       */
63      public long run(String[] args) throws Exception {
64          int size = Integer.parseInt(args[0]);
65          int nbatches = Integer.parseInt(args[1]);
66          int ncycles = Integer.parseInt(args[2]);
67          Proxy[][] arrays = genArrays(size, ncycles);
68          StreamBuffer sbuf = new StreamBuffer();
69          ObjectOutputStream oout =
70              new ObjectOutputStream(sbuf.getOutputStream());
71          ObjectInputStream oin =
72              new ObjectInputStream(sbuf.getInputStream());
73  
74          doReps(oout, oin, sbuf, arrays, 1);     // warmup
75  
76          long start = System.currentTimeMillis();
77          doReps(oout, oin, sbuf, arrays, nbatches);
78          return System.currentTimeMillis() - start;
79      }
80  
81      /**
82       * Generate proxy arrays.
83       */
84      Proxy[][] genArrays(int size, int narrays) throws Exception {
85          Class proxyClass =
86              Proxy.getProxyClass(DummyInterface.class.getClassLoader(),
87                      new Class[] { DummyInterface.class });
88          Constructor proxyCons =
89              proxyClass.getConstructor(new Class[] { InvocationHandler.class });
90          Object[] consArgs = new Object[] { new DummyHandler() };
91          Proxy[][] arrays = new Proxy[narrays][size];
92          for (int i = 0; i < narrays; i++) {
93              for (int j = 0; j < size; j++) {
94                  arrays[i][j] = (Proxy) proxyCons.newInstance(consArgs);
95              }
96          }
97          return arrays;
98      }
99  
100     /**
101      * Run benchmark for given number of batches, with given number of cycles
102      * for each batch.
103      */
104     void doReps(ObjectOutputStream oout, ObjectInputStream oin,
105                 StreamBuffer sbuf, Proxy[][] arrays, int nbatches)
106         throws Exception
107     {
108         int ncycles = arrays.length;
109         for (int i = 0; i < nbatches; i++) {
110             sbuf.reset();
111             oout.reset();
112             for (int j = 0; j < ncycles; j++) {
113                 oout.writeObject(arrays[j]);
114             }
115             oout.flush();
116             for (int j = 0; j < ncycles; j++) {
117                 oin.readObject();
118             }
119         }
120     }
121 }